﻿using System;
using System.IO;
using System.ServiceModel.Syndication;
using System.ServiceModel.Web;
using System.Web;
using System.Xml;
using ScottLane.MediaFeeder.ObjectModel;

namespace ScottLane.MediaFeeder.WebService
{
    /// <summary>
    /// A SyndicationItem representing a Song.
    /// </summary>
    public class SongSyndicationItem : SyndicationItem
    {
        // todo - look into supporting the yahoo media enclosure stuff at http://search.yahoo.com/mrss/
        private Song song;

        /// <summary>
        /// Initializes a new instance of the SongSyndicationItem class with the specified Song.
        /// </summary>
        /// <param name="song"></param>
        public SongSyndicationItem(Song song)
        {
            this.song = song;
            this.Title = new TextSyndicationContent(song.Title);
            this.Content = new TextSyndicationContent("Details of the song '" + song.Title + "' by " + song.Artist);
            this.Categories.Add(new SyndicationCategory(song.Genre));

			if (song.DateAdded != DateTime.MinValue)
			{
				this.PublishDate = song.DateAdded;
			}

            string songUrl = Utility.BaseUrl + "/music/songs/" + HttpUtility.UrlEncode(song.SongID);
			this.Id = songUrl;
            this.Links.Add(SyndicationLink.CreateAlternateLink(new Uri(songUrl)));

			if (song.FileName != null)
			{
				string friendlyFileName = Path.GetFileName(song.FileName).Replace("#", "");
				this.Links.Add(SyndicationLink.CreateMediaEnclosureLink(new Uri(songUrl + "/file/" + friendlyFileName), MimeType.FromExtension(Path.GetExtension(friendlyFileName)), song.FileSize));
			}
        }

        protected override void WriteElementExtensions(XmlWriter writer, string version)
        {
            if (song.Artist != null)
            {
                writer.WriteElementString("mf:artist", song.Artist);
            }
            if (song.Title != null)
            {
                writer.WriteElementString("mf:feedTitle", song.Title);
            }
            if (song.AlbumArtist != null)
            {
                writer.WriteElementString("mf:albumArtist", song.AlbumArtist);
            }
            if (song.AlbumTitle != null)
            {
                writer.WriteElementString("mf:albumTitle", song.AlbumTitle);
            }
            if (song.Year != 0)
            {
                writer.WriteElementString("mf:year", song.Year.ToString());
            }
            if (song.Genre != null)
            {
                writer.WriteElementString("mf:genre", song.Genre);
            }
            if (song.TrackNumber != null)
            {
                writer.WriteElementString("mf:trackNumber", song.TrackNumber);
            }
            if (song.Duration != TimeSpan.Zero)
            {
                writer.WriteElementString("mf:duration", song.Duration.ToString());
                writer.WriteElementString("itunes:duration", song.Duration.ToString());
            }
            if (song.FileName != null)
            {
                writer.WriteElementString("mf:fileName", song.FileName);
            }
        }
    }
}
